using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using Catastrophe.Common;
using Catastrophe.Common.FileM;
using Catastrophe.Common.ObjM;

namespace Catastrophe.Log
{
	public class XmlLog : BaseLog, IFileLog, ILog
	{
		private readonly object _lockObj = new object();

		public XmlLog()
			: base(LogTypeEnum.Xml)
		{
		}

		public void WriteLogNormal(LogLevelEnum logType, 
			string msg,
			Exception ex, string user = "",
			[CallerMemberName] string methodName = "", 
			[CallerFilePath] string sourceFilePath = "",
			[CallerLineNumber] int sourceLineNumber = 0)
		{
			lock (this._lockObj)
			{
				try
				{
					DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(15, 6);
					defaultInterpolatedStringHandler.AppendLiteral("调用方法:");
					defaultInterpolatedStringHandler.AppendFormatted(methodName);
					defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
					defaultInterpolatedStringHandler.AppendLiteral("调用文件:");
					defaultInterpolatedStringHandler.AppendFormatted(sourceFilePath);
					defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
					defaultInterpolatedStringHandler.AppendLiteral("调用行号:");
					defaultInterpolatedStringHandler.AppendFormatted(sourceLineNumber);
					defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
					string text = defaultInterpolatedStringHandler.ToStringAndClear();
					string value = text + base.GetExStr(logType, msg, ex) + Environment.NewLine + "//////////////////////////////////////";
					string value2 = DateTime.Now.ToString("yyyyMMdd");
					string text2 = AppDomain.CurrentDomain.BaseDirectory + Ggo<MyLogConfigEntity>.GetObj.FileUrl;
					if (!Directory.Exists(text2))
					{
						Directory.CreateDirectory(text2);
					}
					defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(2, 4);
					defaultInterpolatedStringHandler.AppendFormatted(text2);
					defaultInterpolatedStringHandler.AppendLiteral("\\");
					defaultInterpolatedStringHandler.AppendFormatted(logType);
					defaultInterpolatedStringHandler.AppendFormatted(value2);
					defaultInterpolatedStringHandler.AppendLiteral(".");
					defaultInterpolatedStringHandler.AppendFormatted(Ggo<MyLogConfigEntity>.GetObj.ExtType);
					string path = defaultInterpolatedStringHandler.ToStringAndClear();
					using StreamWriter streamWriter = new StreamWriter(path, append: true);
					streamWriter.WriteLine(value);
				}
				catch (Exception)
				{
				}
			}
		}

		public void WriteLogOnce(LogLevelEnum logType,
			string msg,
			Exception ex, 
			string user = "", 
			[CallerMemberName] string methodName = "", [
			CallerFilePath] string sourceFilePath = "", 
			[CallerLineNumber] int sourceLineNumber = 0)
		{
			lock (this._lockObj)
			{
				try
				{
					DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(15, 6);
					defaultInterpolatedStringHandler.AppendLiteral("调用方法:");
					defaultInterpolatedStringHandler.AppendFormatted(methodName);
					defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
					defaultInterpolatedStringHandler.AppendLiteral("调用文件:");
					defaultInterpolatedStringHandler.AppendFormatted(sourceFilePath);
					defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
					defaultInterpolatedStringHandler.AppendLiteral("调用行号:");
					defaultInterpolatedStringHandler.AppendFormatted(sourceLineNumber);
					defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
					string text = defaultInterpolatedStringHandler.ToStringAndClear();
					string value = text + base.GetExStr(logType, msg, ex);
					string value2 = DateTime.Now.ToString("yyyyMMdd");
					string text2 = AppDomain.CurrentDomain.BaseDirectory + Ggo<MyLogConfigEntity>.GetObj.FileUrl;
					if (!Directory.Exists(text2))
					{
						Directory.CreateDirectory(text2);
					}
					defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(6, 4);
					defaultInterpolatedStringHandler.AppendFormatted(text2);
					defaultInterpolatedStringHandler.AppendLiteral("\\");
					defaultInterpolatedStringHandler.AppendFormatted(logType);
					defaultInterpolatedStringHandler.AppendFormatted(value2);
					defaultInterpolatedStringHandler.AppendLiteral("Once.");
					defaultInterpolatedStringHandler.AppendFormatted(Ggo<MyLogConfigEntity>.GetObj.ExtType);
					string path = defaultInterpolatedStringHandler.ToStringAndClear();
					using StreamWriter streamWriter = new StreamWriter(path, append: false);
					streamWriter.WriteLine(value);
				}
				catch (Exception)
				{
				}
			}
		}

		public void WriteLogNormal(string fileName, LogLevelEnum logType, string msg, Exception ex, string user = "", [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
		{
			lock (this._lockObj)
			{
				try
				{
					DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(15, 6);
					defaultInterpolatedStringHandler.AppendLiteral("调用方法:");
					defaultInterpolatedStringHandler.AppendFormatted(methodName);
					defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
					defaultInterpolatedStringHandler.AppendLiteral("调用文件:");
					defaultInterpolatedStringHandler.AppendFormatted(sourceFilePath);
					defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
					defaultInterpolatedStringHandler.AppendLiteral("调用行号:");
					defaultInterpolatedStringHandler.AppendFormatted(sourceLineNumber);
					defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
					string text = defaultInterpolatedStringHandler.ToStringAndClear();
					string value = text + base.GetExStr(logType, msg, ex);
					string text2 = AppDomain.CurrentDomain.BaseDirectory + Ggo<MyLogConfigEntity>.GetObj.FileUrl;
					if (!Directory.Exists(text2))
					{
						Directory.CreateDirectory(text2);
					}
					defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(2, 4);
					defaultInterpolatedStringHandler.AppendFormatted(text2);
					defaultInterpolatedStringHandler.AppendLiteral("\\");
					defaultInterpolatedStringHandler.AppendFormatted(logType);
					defaultInterpolatedStringHandler.AppendFormatted(fileName);
					defaultInterpolatedStringHandler.AppendLiteral(".");
					defaultInterpolatedStringHandler.AppendFormatted(Ggo<MyLogConfigEntity>.GetObj.ExtType);
					string path = defaultInterpolatedStringHandler.ToStringAndClear();
					using StreamWriter streamWriter = new StreamWriter(path, append: true);
					streamWriter.WriteLine(value);
				}
				catch (Exception)
				{
				}
			}
		}

		public void WriteLogOnce(string fileName, LogLevelEnum logType, string msg, Exception ex, string user = "", [CallerMemberName] string methodName = "", [CallerFilePath] string sourceFilePath = "", [CallerLineNumber] int sourceLineNumber = 0)
		{
			lock (this._lockObj)
			{
				try
				{
					DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(15, 6);
					defaultInterpolatedStringHandler.AppendLiteral("调用方法:");
					defaultInterpolatedStringHandler.AppendFormatted(methodName);
					defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
					defaultInterpolatedStringHandler.AppendLiteral("调用文件:");
					defaultInterpolatedStringHandler.AppendFormatted(sourceFilePath);
					defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
					defaultInterpolatedStringHandler.AppendLiteral("调用行号:");
					defaultInterpolatedStringHandler.AppendFormatted(sourceLineNumber);
					defaultInterpolatedStringHandler.AppendFormatted(Environment.NewLine);
					string text = defaultInterpolatedStringHandler.ToStringAndClear();
					string value = text + base.GetExStr(logType, msg, ex);
					string text2 = AppDomain.CurrentDomain.BaseDirectory + Ggo<MyLogConfigEntity>.GetObj.FileUrl;
					if (!Directory.Exists(text2))
					{
						Directory.CreateDirectory(text2);
					}
					defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(2, 4);
					defaultInterpolatedStringHandler.AppendFormatted(text2);
					defaultInterpolatedStringHandler.AppendLiteral("\\");
					defaultInterpolatedStringHandler.AppendFormatted(logType);
					defaultInterpolatedStringHandler.AppendFormatted(fileName);
					defaultInterpolatedStringHandler.AppendLiteral(".");
					defaultInterpolatedStringHandler.AppendFormatted(Ggo<MyLogConfigEntity>.GetObj.ExtType);
					string path = defaultInterpolatedStringHandler.ToStringAndClear();
					using StreamWriter streamWriter = new StreamWriter(path, append: false);
					streamWriter.WriteLine(value);
				}
				catch (Exception)
				{
				}
			}
		}

		public void ClearLog(DateTime? startTime, DateTime? endTime)
		{
			string text = AppDomain.CurrentDomain.BaseDirectory + Ggo<MyLogConfigEntity>.GetObj.FileUrl;
			List<FileInfoEntity> dirAllFiles = FileUtil.GetDirAllFiles(text, new List<string> { Ggo<MyLogConfigEntity>.GetObj.ExtType });
			if (startTime.HasValue && endTime.HasValue)
			{
				List<FileInfoEntity> list = dirAllFiles.Where((FileInfoEntity x) => DateTime.Compare(DateTime.Parse(x.CreateTime), startTime.Value) > 0 && DateTime.Compare(endTime.Value, DateTime.Parse(x.CreateTime)) > 0).ToList();
				foreach (FileInfoEntity item in list)
				{
					if (File.Exists(item.FullName))
					{
						File.Delete(item.FullName);
					}
				}
			}
			else
			{
				FileUtil.DeleteFolder(text);
			}
		}

		public DataTable QueryLog(string errorLevel, string definemsg, string timeStart, string timeEnd)
		{
			try
			{
				string item = (Ggo<MyLogConfigEntity>.GetObj.ExtType.StartsWith(".") ? Ggo<MyLogConfigEntity>.GetObj.ExtType : ("." + Ggo<MyLogConfigEntity>.GetObj.ExtType));
				List<FileInfoEntity> dirAllFiles = FileUtil.GetDirAllFiles(AppDomain.CurrentDomain.BaseDirectory + Ggo<MyLogConfigEntity>.GetObj.FileUrl, new List<string> { item });
				dirAllFiles.Where((FileInfoEntity x) => x.NameWithoutExtension.IndexOf(errorLevel) > 0 && DateTime.Compare(DateTime.Parse(x.CreateTime), DateTime.Parse(timeStart)) > 0 && DateTime.Compare(DateTime.Parse(timeEnd), DateTime.Parse(x.CreateTime)) > 0);
				return DataConverter.ConvertToDataT(dirAllFiles);
			}
			catch (Exception innerException)
			{
				throw new Exception("获取异常信息失败!", innerException);
			}
		}
	}
}
